package cn.edu.cumt.ec.securitymybatistest.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import cn.edu.cumt.ec.securitymybatistest.pojo.Role;
import cn.edu.cumt.ec.securitymybatistest.pojo.User;

public interface UserMapper {
	
	// 根据loginName查询用户信息，同时关联查询出用户的权限
	@Select("select * from user where pid = #{pid}")
	 @Results({  
	        @Result(id=true,column="id",property="id"),  
	        @Result(column="pid",property="pid"),
	        @Result(column="mobile_number",property="mobileNumber"),
	        @Result(column="user_sn",property="userSn"),
			 @Result(column="user_name",property="userName"),
			 @Result(column="pid",property="roles",
	        many=@Many(select="findRoleByUser",
	        fetchType=FetchType.EAGER))  
	     })
	User findByLoginName(String loginName);
	
	// 根据用户id关联查询用户的所有权限
	@Select(" SELECT B.id,B.role_sn,B.role_name FROM user_role A inner join role B on A.role_sn=B.role_sn"
			+ " WHERE A.pid = #{pid}")
	@Results({
			@Result(id=true,column="id",property="id"),
			@Result(column="role_sn",property="roleSn"),
			@Result(column="roleName",property="roleName")
	})
	List<Role> findRoleByUser(String pid);

}
